package cn.xhb.service.impl;

import cn.xhb.domain.Role;
import cn.xhb.domain.User;
import cn.xhb.mapper.RoleMapper;
import cn.xhb.mapper.UserMapper;
import cn.xhb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    public List<User> list() {
        List<User> userList = userMapper.findAll();

        for (User user : userList) {
            Long id = user.getId();
          List<Role> roles= roleMapper.findRoleById(id);
          user.setRoles(roles);
        }
        return userList;
    }

    public void save(User user, Long[] roleIds) {
        //第一步 向sys_user表中存储数据
        userMapper.save(user);
        Long userId=user.getId();
        //第二步 向sys_user_role 关系表中存储多条数据
        for (Long roleId : roleIds) {
            userMapper.saveUserRoleRel(userId,roleId);
        }

}

    public void del(Long userId) {
        userMapper.delUserRoleRle(userId);
        userMapper.del(userId);
    }
}
